#include"tools.h"
#include<string.h>
#include<getch.h>
//清理输入缓冲区
void clear_stdin(void)
{
	stdin->_IO_read_ptr = stdin->_IO_read_end;
}


//获取指令

int get_cmd(char start,char end)
{
	puts("--------------------");
	printf("请输入指令：");
	int cmd  = 0;
	for(;;)
	{
		cmd = getch();
		if(cmd >= start && cmd <= end)
		{
			printf("%c\n",cmd);
			break;
		}
	}
	return cmd;
}

//获取字符串
char* get_str(char* str,size_t len)
{
	clear_stdin();
	fgets(str,len,stdin);
	size_t size = strlen(str);
	if('\n' == str[size-1])
	{
		str[size-1] = '\0';
	}
	else clear_stdin();
	return str;
}

//获取密码
char* get_pass(char* pass,size_t len,bool flag)
{
	clear_stdin();
	size_t index = 0;
	while(index < len)
	{
		char key = getch();
		if(127 == key)
		{
			if(index > 0)
			{
				index--;
				if(flag) printf("\b \b");
			}
			continue;
		}
		if('\n' == key) break;
		pass[index++] = key;
		if(flag) printf("*");
	}
	pass[index] = '\0';
	return pass;
}
//按任意键继续
void anykey_continue(void)
{
	printf("按任意键继续\n");
	getch();
	clear_stdin();
}

